| Conditions | 2 |
| Paths | 8 |
| Total Lines | 99 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | ////////////////////////////////////////////////////////////////////////////////////// |
||
| 152 | function draw(str, to, first, box) { |
||
| 153 | tag || init(); |
||
| 154 | |||
| 155 | var re = /("(?:((?:https?|file):\/\/(?:\\?\S)+?)|(?:\\?.)*?)")\s*(:?)|-?\d+\.?\d*(?:e[+-]?\d+)?|true|false|null|[[\]{},]|(\S[^-[\]{},"\d]*)/gi |
||
| 156 | , node = div.cloneNode() |
||
| 157 | , link = document.createElement("a") |
||
| 158 | , span = document.createElement("span") |
||
| 159 | , info = document.createElement("i") |
||
| 160 | , colon = document.createTextNode(": ") |
||
| 161 | , comma = fragment(",") |
||
| 162 | , path = [] |
||
| 163 | , cache = { |
||
| 164 | "{": fragment("{", "}"), |
||
| 165 | "[": fragment("[", "]") |
||
| 166 | }; |
||
| 167 | |||
| 168 | node.className = "R" + rand + (box ? " " + box : ""); |
||
| 169 | |||
| 170 | link.classList.add("L" + rand); |
||
| 171 | info.classList.add("I" + rand); |
||
| 172 | |||
| 173 | to.addEventListener("click", function(e) { |
||
| 174 | var target = e.target, open = target.classList.contains(COLL); |
||
| 175 | if (target.tagName == "I") { |
||
| 176 | if (e.altKey) { |
||
| 177 | changeSiblings(target, COLL, !open); |
||
| 178 | } else if (e[mod]) { |
||
| 179 | open = target.nextSibling.querySelector("i"); |
||
| 180 | if (open) change(target.nextSibling, "i", COLL, !open.classList.contains(COLL)); |
||
| 181 | } else { |
||
| 182 | target.classList[open ? "remove" : "add"](COLL); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | }, true); |
||
| 186 | |||
| 187 | to.replaceChild(box = node, first); |
||
| 188 | loop(str, re); |
||
| 189 | |||
| 190 | function loop(str, re) { |
||
| 191 | str = reconvert(str); |
||
| 192 | var match, val, tmp, i = 0, len = str.length; |
||
| 193 | try { |
||
| 194 | for (; match = re.exec(str); ) { |
||
| 195 | val = match[0]; |
||
| 196 | if (val == "{" || val == "[") { |
||
| 197 | path.push(node); |
||
| 198 | node.appendChild(cache[val].cloneNode(true)); |
||
| 199 | node = node.lastChild.previousSibling; |
||
| 200 | node.len = 1; |
||
| 201 | node.start = re.lastIndex; |
||
| 202 | } else if ((val == "}" || val == "]") && node.len) { |
||
| 203 | if (node.childNodes.length) { |
||
| 204 | tmp = info.cloneNode(); |
||
| 205 | tmp.dataset.content = node.len + ( |
||
| 206 | node.len == 1 ? |
||
| 207 | (val == "]" ? " item, " : " property, ") : |
||
| 208 | (val == "]" ? " items, " : " properties, ") |
||
| 209 | ) + units(re.lastIndex - node.start + 1); |
||
| 210 | |||
| 211 | if ((val = node.previousElementSibling) && val.className == KEY) { |
||
| 212 | tmp.dataset.key = reconvert(val.textContent.slice(1, -1).replace(/'/, "\\'")); |
||
| 213 | } |
||
| 214 | node.parentNode.insertBefore(tmp, node); |
||
| 215 | } else { |
||
| 216 | node.parentNode.removeChild(node); |
||
| 217 | } |
||
| 218 | node = path.pop(); |
||
| 219 | } else if (val == ",") { |
||
| 220 | node.len += 1; |
||
| 221 | node.appendChild(comma.cloneNode(true)); |
||
| 222 | } else { |
||
| 223 | if (match[2]) { |
||
| 224 | tmp = link.cloneNode(); |
||
| 225 | tmp.href = match[2].replace(/\\"/g, '"'); |
||
| 226 | } else { |
||
| 227 | tmp = span.cloneNode(); |
||
| 228 | } |
||
| 229 | tmp.textContent = match[1] || val; |
||
| 230 | tmp.classList.add(match[3] ? KEY : match[1] ? STR : match[4] ? ERR : BOOL); |
||
| 231 | node.appendChild(tmp); |
||
| 232 | if (match[3]) { |
||
| 233 | node.appendChild(colon.cloneNode()); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | if (++i > 1000) { |
||
| 237 | document.title = (0|(100*re.lastIndex/len)) + "% of " + units(len); |
||
| 238 | return setTimeout(function() { loop(str, re) }); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | document.title = "" |
||
| 242 | JSON.parse(str) |
||
| 243 | } catch(e) { |
||
| 244 | tmp = document.createElement("h3"); |
||
| 245 | tmp.className = ERR; |
||
| 246 | tmp.textContent = e; |
||
| 247 | box.insertBefore(tmp, box.firstChild); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 289 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.